package com.zyb.patient.activity;

import java.util.LinkedHashMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.zyb.patient.IBaseCallBack;
import com.zyb.patient.R;
import com.zyb.patient.dto.UserDto;
import com.zyb.patient.service.DbSqliteService;
import com.zyb.patient.service.MainService;
import com.zyb.patient.service.Task;
import com.zyb.patient.util.AppUtils;
import com.zyb.patient.util.Constant;
import com.zyb.patient.util.LogUtil;

public class LoadingActivity extends BaseActivity implements IBaseCallBack {

	public Context mContext;

	@Override
	public void refresh(Object... param) {
		try {
			int operType = (Integer) param[0];
			Object obj = param[1];
			switch (operType) {
			case Task.GET_INIT_DATA:
				if (obj != null) {
					UserDto dto = (UserDto) obj;
					if ("1".equals(dto.result)) {
						db.modifyConfigItem(DbSqliteService.USER_ID, dto.uid);
						db.insertUserInfo(dto);
					} else
						LogUtil.e("网络链接异常" + dto.message);
				} else {
					LogUtil.e("网络链接异常，更新数据失败");
					showToast(R.string.about_http_no_net_tip);
				}

				break;

			case Task.SEND_PUSH_USERID:
				Log.e("LoadingActivity", "发送userid成功");
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		MainService.addActivity(this);

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_loading);

		ImageView logobackground = (ImageView) this
				.findViewById(R.id.load_imageview);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0.6f, 1.0f);
		alphaAnimation.setDuration(3000);
		logobackground.startAnimation(alphaAnimation);
		alphaAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				goNext();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationStart(Animation animation) {
				/**
				 * 开始获取APP初始化数据 eg:主动拉版本升级信息 更新本地数据库(城市列表 课程表库等)
				 * **/
				getInitData();
			}
		});
	}

	private void getInitData() {
		/** 登录状态才获取 用户相关信息 **/
		if (!"0".equals(db.getConfigItem(DbSqliteService.USER_ID))) {
			LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			map.put("s", "main/get_initialize");
			map.put("device_code", AppUtils.getmDeviceID());
			map.put("token", "token");
			getData(map, Task.GET_INIT_DATA, 0);
		}	
	}

	private void goNext() {
		Intent it;
		LogUtil.e("uid:" + db.getConfigItem(DbSqliteService.USER_ID));
		if ("0".equals(db.getConfigItem(DbSqliteService.USER_ID))) {
			if ("0".equals(db.getConfigItem("isFirst"))) {
				it = new Intent(LoadingActivity.this, GuideActivity.class);
				it.putExtra("source", "0");
			} else {
				it = new Intent(LoadingActivity.this, UserLoginActivity.class);
				it.putExtra("source", "0");
			}
		} else {
			it = new Intent(LoadingActivity.this, MainActivity.class);
		}
		startActivity(it);
		finish();
	}

}
